package com.samoilovich.belarustoday.afisha.films;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.basefragments.BaseFragment;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.adapters.FilmAdapter;
import com.samoilovich.belarustoday.afisha.AfishaActivity;
import com.samoilovich.belarustoday.calendar.fragments.CalendarHelper;
import com.samoilovich.belarustoday.calendar.fragments.ICalendarListener;
import com.samoilovich.belarustoday.datemodel.FilmModel;
import com.samoilovich.belarustoday.handlers.AfishaContentHandler;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;
import java.util.Date;

import hirondelle.date4j.DateTime;

public class FilmsFragment extends BaseFragment implements ICalendarListener {
    private static ArrayList<FilmModel> sFilms;
    private FilmAdapter mAdapter;
    private String mDate;

    private ListView mListView;
    private ProgressBar mProgressBarContentWait;
    private View mViewNoConnection;
    private View.OnClickListener mFilmSeancesClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            FilmModel film = mAdapter.getItem(position);
            ((AfishaActivity) getActivity()).setFilmSeancesFragment(film, mDate);
        }
    };
    private View.OnClickListener mFilmDescriptionClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            FilmModel film = mAdapter.getItem(position);
            ((AfishaActivity) getActivity()).setFilmDescriptionFragment(film);
        }
    };

    public FilmsFragment() {

    }

    public FilmsFragment(Context context, String tag) {
        mInProgress = false;
        mDate = CalendarHelper.getTodayAsString();
        getFilmsList(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_main, null);
        prepareActionBarViews(R.string.films, R.string.afisha_label, true);
        prepareViews();

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sFilms != null) {
            updateScreen();
        }
    }

    public void updateFilmsList(final ArrayList<FilmModel> films) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sFilms = films;
                updateScreen();
                mInProgress = false;
            }
        });
    }

    @Override
    public void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        if (mListView == null || mProgressBarContentWait == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        if (intent.getAction().equals(Constants.PICTURE_LOADING_ACTION)) {
            mAdapter.setPictureLoadingState(Utility.getPictureLoadingState(getActivity()));
        } else {
            if (hasInternetConnection) {
                mViewNoConnection.setVisibility(View.GONE);
                if (!mInProgress && sFilms == null) {
                    getFilmsList(getActivity());
                }
            } else {
                if (sFilms == null) {
                    mViewNoConnection.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void updateAllData(Context context) {
        if (mListView == null || mRootView == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.GONE);
        mProgressBarContentWait.setVisibility(View.VISIBLE);
        mViewNoConnection.setVisibility(View.GONE);
        getFilmsList(context);
    }

    @Override
    public void onSelectDate(Date date, View view) {
        ((AfishaActivity) getActivity()).dismissCalendarDialog();
        mDate = CalendarHelper.getDateAsString(date);
        updateAllData(getActivity());
    }

    @Override
    public void onLongClickDate(Date date, View view) {
    }

    @Override
    public void onChangeMonth(int month, int year) {
    }

    @Override
    public void onCalendarViewCreated() {
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void prepareViews() {
        mProgressBarContentWait = (ProgressBar) mRootView.findViewById(R.id.progressBar_categoryWait);
        mListView = (ListView) mRootView.findViewById(R.id.listView_category);
        mViewNoConnection = mRootView.findViewById(R.id.view_noConnection);
        mAdapter = new FilmAdapter(getActivity(), null, R.layout.film_list_item, mFilmDescriptionClickListener, mFilmSeancesClickListener);
        mListView.setAdapter(mAdapter);

        Button calendarButton = (Button) mRootView.findViewById(R.id.button_calendar);
        calendarButton.setVisibility(View.VISIBLE);
        calendarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mDate == null) {
                    mDate = CalendarHelper.getTodayAsString();
                }
                DateTime clickedDate = CalendarHelper.getDateTimeFromString(mDate, null);
                ((AfishaActivity) getActivity()).setCalendarFilmsFragment(FilmsFragment.this, clickedDate);
            }
        });
    }

    private void updateScreen() {
        if (mListView == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.VISIBLE);
        mProgressBarContentWait.setVisibility(View.GONE);
        mAdapter.setFilms(sFilms);
    }

    private void getFilmsList(Context context) {
        sFilms = null;
        if (Utility.hasInternetConnection(context)) {
            mInProgress = true;
            if (mDate == null) {
                mDate = CalendarHelper.getTodayAsString();
            }
            AfishaContentHandler.getInstance().getFilmsList(this, mDate);
        }
    }

}
